<?php

namespace Extend\Controller;
use Think\Controller\RpcController;


/**
 * rpcController父类
 * @modiy trainyao 2016-05-06 创建
 */
class ParentController extends RpcController {

    protected function _initialize() {
        $this->switchTimeUtc();
    }

    /**
     * 言切时区
     * @modify wenhui 2017-06-22    创建
     */
    protected function switchTimeUtc($data) {
        if (isset($data['hotel'])) {
            $hotel = $data['hotel'];
        } else if (isset($data['hotelId'])) {
            $hotel = $data['hotelId'];
        } else{
            date_default_timezone_set('Etc/GMT-8');
            return;
        }

        $hotelEntityInfo= D('HotelEntity')->getInfo($hotel);
        $universalTimeModel = D('UniversalTime');
        $universalTimeInfo = $universalTimeModel->field('etc')->where(['id' => $hotelEntityInfo['universaltime_id']])->find();
        $universalTimeEtc = $universalTimeInfo ? $universalTimeInfo['etc'] : 'Etc/GMT-8';
        date_default_timezone_set($universalTimeEtc);
        return;
    }

    /**
     * 检查是否有权限调用rpc接口
     * @modify trainyao 2016-05-06 创建
     * @param  array   $data       RPC传入数据
     * @return boolean             是否有权限调用rpc接口
     */
    public function checkRPCRight($data) {
        $currentTime = time();
        if (!isset($data['time'])) {
            return false;
        }
        $data['time'] = !is_numeric($data['time']) ? strtotime($data['time']) : $data['time'];
        if ($currentTime - $data['time'] >= 60) {
            return false;
        }
        if ($data['flag'] != C('MEIZHU_RPC_FLAG')) {
            return false;
        }
        return true;
    }

    /**
     * RPC返回数据格式
     * @modify trainyao 2016-05-06  创建
     * @param  integer   $httpStatus http状态码
     * @param  integer   $status     业务状态码
     * @return array               array('httpstatus' => integer, 'status' => integer, 'data' => mixin, 'info' => string)
     */
    public function returnJson($httpStatus, $status = null, $data = null) {
        if (!is_numeric($httpStatus) && is_null($status)) {
            $httpStatus = explode(',', $httpStatus);
            $status = $httpStatus[1];
            $httpStatus = $httpStatus[0];
        }
        
        return array(
            'httpstatus' => $httpStatus,
            'status' => $status,
            'data' => is_null($data) ? array() : $data,
            'info' => \Org\Util\LogicStatus::getInfo($httpStatus, $status),
        );
    }

    /**
     * @description 是否是义卖客栈 
     * @modify wenhui 2017-09-12
     * @param int $hotelId 客栈id
     * @param int $maxRooms 最大房间数
     * @param int $count 房间数量
     */
    protected function checkUsageHotel($hotelId) {
        $hotelInfo = D('HotelEntity')->getInfo($hotelId);
        $roomInfo = D('RoomInfo')->getInfoByHotel($hotelId);
        $isMax = false;
        $isFreeHotel = false;
        if ($hotelInfo['usage'] == 4) {
            $isFreeHotel = true;
        }
        $existsRooms = isset($roomInfo) ? count($roomInfo) : 0;

        return array('isFreeHotel' => $isFreeHotel , 'existsRooms' => $existsRooms);
    }

    /**
     * @description 是否开启畅联代收 畅联代收是否过期
     * @modify wenhui 2017-09-12
     */
    protected function checkIsUclbrtPay($hotelId) {
        $hotelPayDetailInfo = D('HotelPayDetail')->getInfoByHotel($hotelId);
        $hotelPayUsableInfo = D('HotelPayUsable')->getInfo($hotelId);

        $isPayUclbrt = false;
        $currentDate = date('Y-m-d');
        if ($hotelPayUsableInfo['uclbrtalipayenable'] == 1 && $hotelPayUsableInfo['uclbrtalipayenddate'] > $currentDate) {
            // 代收 和 自收
            if ($hotelPayDetailInfo['type'] == '33') {
                if ($hotelPayUsableInfo['hotelwechatenable'] == 1 && $hotelPayUsableInfo['hotelwechatenddata'] > $currentDate) {
                    $isPayUclbrt = false;
                } else {
                    $isPayUclbrt = true;
                }
            } else if (in_array($hotelPayDetailInfo['type'], array('1','3'))) {
                $isPayUclbrt = true;
            }
        }
        if (!$isPayUclbrt) {
            if ($hotelPayUsableInfo['uclbrtwechatenable'] == 1 && $hotelPayUsableInfo['uclbrtwechatenddate'] > $currentDate) {
                // 代收 和 自收
                if ($hotelPayDetailInfo['type'] == '18') {
                    if ($hotelPayUsableInfo['hotelalipayenable'] == 1 && $hotelPayUsableInfo['hotelalipayenddate'] > $currentDate) {
                        $isPayUclbrt = false;
                    } else {
                        $isPayUclbrt = true;
                    }
                } else if (in_array($hotelPayDetailInfo['type'], array('2','3'))) {
                    $isPayUclbrt = true;
                }
            }
        }

        return $isPayUclbrt;
    }

    /**
     * 发送请求到后台进行推送消息，提醒客户端
     * @modify wenhui 2017-09-28
     * @param integer $hotel 客栈id
     * @param integer $roomId 房间id
     */
    protected function pushNewsTask($hotel, $roomId) {
        $fileLog = new \Org\Util\FileLog();
        // 定义数据类型格式
        $data = array(
            'hotel' => $hotel,
            'roomId' => $roomId,
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://' . C('SOCKET_SERVER_NEWS_IP') . ':' . C('SOCKET_SERVER_NEWS_PORT'));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);

        if ($result != 'success') {
            $fileLog->write('push failed!' . json_encode($data), 0, RUNTIME_PATH . "/Logs/Home/" . date('Y-m-d') . "_pushTask.log");
            return false;
        }
        return true;
    }
}
